/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.samples.spring;

import io.sentry.SentryOptions.TracesSamplerCallback;
import io.sentry.spring.EnableSentry;
import io.sentry.spring.tracing.SentryTracingConfiguration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * SentryConfig
 *
 * @since 2021-05-24
 */
@EnableSentry(
    dsn = "https://502f25099c204a2fbf4cb16edc5975d1@o447951.ingest.sentry.io/5428563",
    sendDefaultPii = true)
@Import(SentryTracingConfiguration.class)
public class SentryConfig {
    /**
     * Configures callback used to determine if transaction should be sampled.
     *
     * @return traces sampler callback
     */
    @Bean
    TracesSamplerCallback tracesSamplerCallback() {
        return samplingContext -> {
            HttpServletRequest request =
                (HttpServletRequest) samplingContext.getCustomSamplingContext().get("request");
            if ("/error".equals(request.getRequestURI())) {
                return 0.5d;
            } else {
                return 1.0d;
            }
        };
    }
}
